home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / python2.4 / test / test_posixpath.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2005-10-18  |  15KB  |  512 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.4)
  3.  
  4. import unittest
  5. from test import test_support
  6. import posixpath
  7. import os
  8. from posixpath import realpath, abspath, join, dirname, basename
  9. ABSTFN = abspath(test_support.TESTFN)
  10.  
  11. class PosixPathTest(unittest.TestCase):
  12.     
  13.     def assertIs(self, a, b):
  14.         self.assert_(a is b)
  15.  
  16.     
  17.     def test_normcase(self):
  18.         p = 'FoO/./BaR'
  19.         p = posixpath.normcase(p)
  20.         self.assertEqual(p, posixpath.normcase(p))
  21.         self.assertRaises(TypeError, posixpath.normcase)
  22.  
  23.     
  24.     def test_join(self):
  25.         self.assertEqual(posixpath.join('/foo', 'bar', '/bar', 'baz'), '/bar/baz')
  26.         self.assertEqual(posixpath.join('/foo', 'bar', 'baz'), '/foo/bar/baz')
  27.         self.assertEqual(posixpath.join('/foo/', 'bar/', 'baz/'), '/foo/bar/baz/')
  28.         self.assertRaises(TypeError, posixpath.join)
  29.  
  30.     
  31.     def test_splitdrive(self):
  32.         self.assertEqual(posixpath.splitdrive('/foo/bar'), ('', '/foo/bar'))
  33.         self.assertRaises(TypeError, posixpath.splitdrive)
  34.  
  35.     
  36.     def test_split(self):
  37.         self.assertEqual(posixpath.split('/foo/bar'), ('/foo', 'bar'))
  38.         self.assertEqual(posixpath.split('/'), ('/', ''))
  39.         self.assertEqual(posixpath.split('foo'), ('', 'foo'))
  40.         self.assertEqual(posixpath.split('////foo'), ('////', 'foo'))
  41.         self.assertEqual(posixpath.split('//foo//bar'), ('//foo', 'bar'))
  42.         self.assertRaises(TypeError, posixpath.split)
  43.  
  44.     
  45.     def test_splitext(self):
  46.         self.assertEqual(posixpath.splitext('foo.ext'), ('foo', '.ext'))
  47.         self.assertEqual(posixpath.splitext('/foo/foo.ext'), ('/foo/foo', '.ext'))
  48.         self.assertEqual(posixpath.splitext('.ext'), ('', '.ext'))
  49.         self.assertEqual(posixpath.splitext('/foo.ext/foo'), ('/foo.ext/foo', ''))
  50.         self.assertEqual(posixpath.splitext('foo.ext/'), ('foo.ext/', ''))
  51.         self.assertEqual(posixpath.splitext(''), ('', ''))
  52.         self.assertEqual(posixpath.splitext('foo.bar.ext'), ('foo.bar', '.ext'))
  53.         self.assertRaises(TypeError, posixpath.splitext)
  54.  
  55.     
  56.     def test_isabs(self):
  57.         self.assertIs(posixpath.isabs(''), False)
  58.         self.assertIs(posixpath.isabs('/'), True)
  59.         self.assertIs(posixpath.isabs('/foo'), True)
  60.         self.assertIs(posixpath.isabs('/foo/bar'), True)
  61.         self.assertIs(posixpath.isabs('foo/bar'), False)
  62.         self.assertRaises(TypeError, posixpath.isabs)
  63.  
  64.     
  65.     def test_splitdrive(self):
  66.         self.assertEqual(posixpath.splitdrive('/foo/bar'), ('', '/foo/bar'))
  67.         self.assertRaises(TypeError, posixpath.splitdrive)
  68.  
  69.     
  70.     def test_basename(self):
  71.         self.assertEqual(posixpath.basename('/foo/bar'), 'bar')
  72.         self.assertEqual(posixpath.basename('/'), '')
  73.         self.assertEqual(posixpath.basename('foo'), 'foo')
  74.         self.assertEqual(posixpath.basename('////foo'), 'foo')
  75.         self.assertEqual(posixpath.basename('//foo//bar'), 'bar')
  76.         self.assertRaises(TypeError, posixpath.basename)
  77.  
  78.     
  79.     def test_dirname(self):
  80.         self.assertEqual(posixpath.dirname('/foo/bar'), '/foo')
  81.         self.assertEqual(posixpath.dirname('/'), '/')
  82.         self.assertEqual(posixpath.dirname('foo'), '')
  83.         self.assertEqual(posixpath.dirname('////foo'), '////')
  84.         self.assertEqual(posixpath.dirname('//foo//bar'), '//foo')
  85.         self.assertRaises(TypeError, posixpath.dirname)
  86.  
  87.     
  88.     def test_commonprefix(self):
  89.         self.assertEqual(posixpath.commonprefix([]), '')
  90.         self.assertEqual(posixpath.commonprefix([
  91.             '/home/swenson/spam',
  92.             '/home/swen/spam']), '/home/swen')
  93.         self.assertEqual(posixpath.commonprefix([
  94.             '/home/swen/spam',
  95.             '/home/swen/eggs']), '/home/swen/')
  96.         self.assertEqual(posixpath.commonprefix([
  97.             '/home/swen/spam',
  98.             '/home/swen/spam']), '/home/swen/spam')
  99.  
  100.     
  101.     def test_getsize(self):
  102.         f = open(test_support.TESTFN, 'wb')
  103.         
  104.         try:
  105.             f.write('foo')
  106.             f.close()
  107.             self.assertEqual(posixpath.getsize(test_support.TESTFN), 3)
  108.         finally:
  109.             if not f.closed:
  110.                 f.close()
  111.             
  112.             os.remove(test_support.TESTFN)
  113.  
  114.  
  115.     
  116.     def test_time(self):
  117.         f = open(test_support.TESTFN, 'wb')
  118.         
  119.         try:
  120.             f.write('foo')
  121.             f.close()
  122.             f = open(test_support.TESTFN, 'ab')
  123.             f.write('bar')
  124.             f.close()
  125.             f = open(test_support.TESTFN, 'rb')
  126.             d = f.read()
  127.             f.close()
  128.             self.assertEqual(d, 'foobar')
  129.             self.assert_(posixpath.getctime(test_support.TESTFN) <= posixpath.getmtime(test_support.TESTFN))
  130.         finally:
  131.             if not f.closed:
  132.                 f.close()
  133.             
  134.             os.remove(test_support.TESTFN)
  135.  
  136.  
  137.     
  138.     def test_islink(self):
  139.         self.assertIs(posixpath.islink(test_support.TESTFN + '1'), False)
  140.         f = open(test_support.TESTFN + '1', 'wb')
  141.         
  142.         try:
  143.             f.write('foo')
  144.             f.close()
  145.             self.assertIs(posixpath.islink(test_support.TESTFN + '1'), False)
  146.             if hasattr(os, 'symlink'):
  147.                 os.symlink(test_support.TESTFN + '1', test_support.TESTFN + '2')
  148.                 self.assertIs(posixpath.islink(test_support.TESTFN + '2'), True)
  149.                 os.remove(test_support.TESTFN + '1')
  150.                 self.assertIs(posixpath.islink(test_support.TESTFN + '2'), True)
  151.                 self.assertIs(posixpath.exists(test_support.TESTFN + '2'), False)
  152.                 self.assertIs(posixpath.lexists(test_support.TESTFN + '2'), True)
  153.         finally:
  154.             if not f.close():
  155.                 f.close()
  156.             
  157.             
  158.             try:
  159.                 os.remove(test_support.TESTFN + '1')
  160.             except os.error:
  161.                 pass
  162.  
  163.             
  164.             try:
  165.                 os.remove(test_support.TESTFN + '2')
  166.             except os.error:
  167.                 pass
  168.  
  169.  
  170.         self.assertRaises(TypeError, posixpath.islink)
  171.  
  172.     
  173.     def test_exists(self):
  174.         self.assertIs(posixpath.exists(test_support.TESTFN), False)
  175.         f = open(test_support.TESTFN, 'wb')
  176.         
  177.         try:
  178.             f.write('foo')
  179.             f.close()
  180.             self.assertIs(posixpath.exists(test_support.TESTFN), True)
  181.             self.assertIs(posixpath.lexists(test_support.TESTFN), True)
  182.         finally:
  183.             if not f.close():
  184.                 f.close()
  185.             
  186.             
  187.             try:
  188.                 os.remove(test_support.TESTFN)
  189.             except os.error:
  190.                 pass
  191.  
  192.  
  193.         self.assertRaises(TypeError, posixpath.exists)
  194.  
  195.     
  196.     def test_isdir(self):
  197.         self.assertIs(posixpath.isdir(test_support.TESTFN), False)
  198.         f = open(test_support.TESTFN, 'wb')
  199.         
  200.         try:
  201.             f.write('foo')
  202.             f.close()
  203.             self.assertIs(posixpath.isdir(test_support.TESTFN), False)
  204.             os.remove(test_support.TESTFN)
  205.             os.mkdir(test_support.TESTFN)
  206.             self.assertIs(posixpath.isdir(test_support.TESTFN), True)
  207.             os.rmdir(test_support.TESTFN)
  208.         finally:
  209.             if not f.close():
  210.                 f.close()
  211.             
  212.             
  213.             try:
  214.                 os.remove(test_support.TESTFN)
  215.             except os.error:
  216.                 pass
  217.  
  218.             
  219.             try:
  220.                 os.rmdir(test_support.TESTFN)
  221.             except os.error:
  222.                 pass
  223.  
  224.  
  225.         self.assertRaises(TypeError, posixpath.isdir)
  226.  
  227.     
  228.     def test_isfile(self):
  229.         self.assertIs(posixpath.isfile(test_support.TESTFN), False)
  230.         f = open(test_support.TESTFN, 'wb')
  231.         
  232.         try:
  233.             f.write('foo')
  234.             f.close()
  235.             self.assertIs(posixpath.isfile(test_support.TESTFN), True)
  236.             os.remove(test_support.TESTFN)
  237.             os.mkdir(test_support.TESTFN)
  238.             self.assertIs(posixpath.isfile(test_support.TESTFN), False)
  239.             os.rmdir(test_support.TESTFN)
  240.         finally:
  241.             if not f.close():
  242.                 f.close()
  243.             
  244.             
  245.             try:
  246.                 os.remove(test_support.TESTFN)
  247.             except os.error:
  248.                 pass
  249.  
  250.             
  251.             try:
  252.                 os.rmdir(test_support.TESTFN)
  253.             except os.error:
  254.                 pass
  255.  
  256.  
  257.         self.assertRaises(TypeError, posixpath.isdir)
  258.         
  259.         def test_samefile(self):
  260.             f = open(test_support.TESTFN + '1', 'wb')
  261.             
  262.             try:
  263.                 f.write('foo')
  264.                 f.close()
  265.                 self.assertIs(posixpath.samefile(test_support.TESTFN + '1', test_support.TESTFN + '1'), True)
  266.                 if hasattr(os, 'symlink'):
  267.                     os.symlink(test_support.TESTFN + '1', test_support.TESTFN + '2')
  268.                     self.assertIs(posixpath.samefile(test_support.TESTFN + '1', test_support.TESTFN + '2'), True)
  269.                     os.remove(test_support.TESTFN + '2')
  270.                     f = open(test_support.TESTFN + '2', 'wb')
  271.                     f.write('bar')
  272.                     f.close()
  273.                     self.assertIs(posixpath.samefile(test_support.TESTFN + '1', test_support.TESTFN + '2'), False)
  274.             finally:
  275.                 if not f.close():
  276.                     f.close()
  277.                 
  278.                 
  279.                 try:
  280.                     os.remove(test_support.TESTFN + '1')
  281.                 except os.error:
  282.                     pass
  283.  
  284.                 
  285.                 try:
  286.                     os.remove(test_support.TESTFN + '2')
  287.                 except os.error:
  288.                     pass
  289.  
  290.  
  291.             self.assertRaises(TypeError, posixpath.samefile)
  292.  
  293.  
  294.     
  295.     def test_samestat(self):
  296.         f = open(test_support.TESTFN + '1', 'wb')
  297.         
  298.         try:
  299.             f.write('foo')
  300.             f.close()
  301.             self.assertIs(posixpath.samestat(os.stat(test_support.TESTFN + '1'), os.stat(test_support.TESTFN + '1')), True)
  302.             if hasattr(os, 'symlink'):
  303.                 if hasattr(os, 'symlink'):
  304.                     os.symlink(test_support.TESTFN + '1', test_support.TESTFN + '2')
  305.                     self.assertIs(posixpath.samestat(os.stat(test_support.TESTFN + '1'), os.stat(test_support.TESTFN + '2')), True)
  306.                     os.remove(test_support.TESTFN + '2')
  307.                 
  308.                 f = open(test_support.TESTFN + '2', 'wb')
  309.                 f.write('bar')
  310.                 f.close()
  311.                 self.assertIs(posixpath.samestat(os.stat(test_support.TESTFN + '1'), os.stat(test_support.TESTFN + '2')), False)
  312.         finally:
  313.             if not f.close():
  314.                 f.close()
  315.             
  316.             
  317.             try:
  318.                 os.remove(test_support.TESTFN + '1')
  319.             except os.error:
  320.                 pass
  321.  
  322.             
  323.             try:
  324.                 os.remove(test_support.TESTFN + '2')
  325.             except os.error:
  326.                 pass
  327.  
  328.  
  329.         self.assertRaises(TypeError, posixpath.samestat)
  330.  
  331.     
  332.     def test_ismount(self):
  333.         self.assertIs(posixpath.ismount('/'), True)
  334.         self.assertRaises(TypeError, posixpath.ismount)
  335.  
  336.     
  337.     def test_expanduser(self):
  338.         self.assertEqual(posixpath.expanduser('foo'), 'foo')
  339.         
  340.         try:
  341.             import pwd as pwd
  342.         except ImportError:
  343.             pass
  344.  
  345.         self.assert_(isinstance(posixpath.expanduser('~/'), basestring))
  346.         if posixpath.expanduser('~') != '/':
  347.             self.assertEqual(posixpath.expanduser('~') + '/', posixpath.expanduser('~/'))
  348.         
  349.         self.assert_(isinstance(posixpath.expanduser('~root/'), basestring))
  350.         self.assert_(isinstance(posixpath.expanduser('~foo/'), basestring))
  351.         self.assertRaises(TypeError, posixpath.expanduser)
  352.  
  353.     
  354.     def test_expandvars(self):
  355.         oldenv = os.environ.copy()
  356.         
  357.         try:
  358.             os.environ.clear()
  359.             os.environ['foo'] = 'bar'
  360.             os.environ['{foo'] = 'baz1'
  361.             os.environ['{foo}'] = 'baz2'
  362.             self.assertEqual(posixpath.expandvars('foo'), 'foo')
  363.             self.assertEqual(posixpath.expandvars('$foo bar'), 'bar bar')
  364.             self.assertEqual(posixpath.expandvars('${foo}bar'), 'barbar')
  365.             self.assertEqual(posixpath.expandvars('$[foo]bar'), '$[foo]bar')
  366.             self.assertEqual(posixpath.expandvars('$bar bar'), '$bar bar')
  367.             self.assertEqual(posixpath.expandvars('$?bar'), '$?bar')
  368.             self.assertEqual(posixpath.expandvars('${foo}bar'), 'barbar')
  369.             self.assertEqual(posixpath.expandvars('$foo}bar'), 'bar}bar')
  370.             self.assertEqual(posixpath.expandvars('${foo'), '${foo')
  371.             self.assertEqual(posixpath.expandvars('${{foo}}'), 'baz1}')
  372.         finally:
  373.             os.environ.clear()
  374.             os.environ.update(oldenv)
  375.  
  376.         self.assertRaises(TypeError, posixpath.expandvars)
  377.  
  378.     
  379.     def test_normpath(self):
  380.         self.assertEqual(posixpath.normpath(''), '.')
  381.         self.assertEqual(posixpath.normpath('/'), '/')
  382.         self.assertEqual(posixpath.normpath('//'), '//')
  383.         self.assertEqual(posixpath.normpath('///'), '/')
  384.         self.assertEqual(posixpath.normpath('///foo/.//bar//'), '/foo/bar')
  385.         self.assertEqual(posixpath.normpath('///foo/.//bar//.//..//.//baz'), '/foo/baz')
  386.         self.assertEqual(posixpath.normpath('///..//./foo/.//bar'), '/foo/bar')
  387.         self.assertRaises(TypeError, posixpath.normpath)
  388.  
  389.     
  390.     def test_abspath(self):
  391.         self.assert_('foo' in posixpath.abspath('foo'))
  392.         self.assertRaises(TypeError, posixpath.abspath)
  393.  
  394.     
  395.     def test_realpath(self):
  396.         self.assert_('foo' in realpath('foo'))
  397.         self.assertRaises(TypeError, posixpath.realpath)
  398.  
  399.     if hasattr(os, 'symlink'):
  400.         
  401.         def test_realpath_basic(self):
  402.             
  403.             try:
  404.                 os.symlink(ABSTFN + '1', ABSTFN)
  405.                 self.assertEqual(realpath(ABSTFN), ABSTFN + '1')
  406.             finally:
  407.                 self.safe_remove(ABSTFN)
  408.  
  409.  
  410.         
  411.         def test_realpath_symlink_loops(self):
  412.             
  413.             try:
  414.                 old_path = abspath('.')
  415.                 os.symlink(ABSTFN, ABSTFN)
  416.                 self.assertEqual(realpath(ABSTFN), ABSTFN)
  417.                 os.symlink(ABSTFN + '1', ABSTFN + '2')
  418.                 os.symlink(ABSTFN + '2', ABSTFN + '1')
  419.                 self.assertEqual(realpath(ABSTFN + '1'), ABSTFN + '1')
  420.                 self.assertEqual(realpath(ABSTFN + '2'), ABSTFN + '2')
  421.                 os.chdir(dirname(ABSTFN))
  422.                 self.assertEqual(realpath(basename(ABSTFN)), ABSTFN)
  423.             finally:
  424.                 os.chdir(old_path)
  425.                 self.safe_remove(ABSTFN)
  426.                 self.safe_remove(ABSTFN + '1')
  427.                 self.safe_remove(ABSTFN + '2')
  428.  
  429.  
  430.         
  431.         def test_realpath_resolve_parents(self):
  432.             
  433.             try:
  434.                 old_path = abspath('.')
  435.                 os.mkdir(ABSTFN)
  436.                 os.mkdir(ABSTFN + '/y')
  437.                 os.symlink(ABSTFN + '/y', ABSTFN + '/k')
  438.                 os.chdir(ABSTFN + '/k')
  439.                 self.assertEqual(realpath('a'), ABSTFN + '/y/a')
  440.             finally:
  441.                 os.chdir(old_path)
  442.                 self.safe_remove(ABSTFN + '/k')
  443.                 self.safe_rmdir(ABSTFN + '/y')
  444.                 self.safe_rmdir(ABSTFN)
  445.  
  446.  
  447.         
  448.         def test_realpath_resolve_before_normalizing(self):
  449.             
  450.             try:
  451.                 old_path = abspath('.')
  452.                 os.mkdir(ABSTFN)
  453.                 os.mkdir(ABSTFN + '/k')
  454.                 os.mkdir(ABSTFN + '/k/y')
  455.                 os.symlink(ABSTFN + '/k/y', ABSTFN + '/link-y')
  456.                 self.assertEqual(realpath(ABSTFN + '/link-y/..'), ABSTFN + '/k')
  457.                 os.chdir(dirname(ABSTFN))
  458.                 self.assertEqual(realpath(basename(ABSTFN) + '/link-y/..'), ABSTFN + '/k')
  459.             finally:
  460.                 os.chdir(old_path)
  461.                 self.safe_remove(ABSTFN + '/link-y')
  462.                 self.safe_rmdir(ABSTFN + '/k/y')
  463.                 self.safe_rmdir(ABSTFN + '/k')
  464.                 self.safe_rmdir(ABSTFN)
  465.  
  466.  
  467.         
  468.         def test_realpath_resolve_first(self):
  469.             
  470.             try:
  471.                 old_path = abspath('.')
  472.                 os.mkdir(ABSTFN)
  473.                 os.mkdir(ABSTFN + '/k')
  474.                 os.symlink(ABSTFN, ABSTFN + 'link')
  475.                 os.chdir(dirname(ABSTFN))
  476.                 base = basename(ABSTFN)
  477.                 self.assertEqual(realpath(base + 'link'), ABSTFN)
  478.                 self.assertEqual(realpath(base + 'link/k'), ABSTFN + '/k')
  479.             finally:
  480.                 os.chdir(old_path)
  481.                 self.safe_remove(ABSTFN + 'link')
  482.                 self.safe_rmdir(ABSTFN + '/k')
  483.                 self.safe_rmdir(ABSTFN)
  484.  
  485.  
  486.         
  487.         def pass_os_error(self, func, filename):
  488.             
  489.             try:
  490.                 func(filename)
  491.             except OSError:
  492.                 pass
  493.  
  494.  
  495.         
  496.         def safe_remove(self, filename):
  497.             self.pass_os_error(os.remove, filename)
  498.  
  499.         
  500.         def safe_rmdir(self, dirname):
  501.             self.pass_os_error(os.rmdir, dirname)
  502.  
  503.     
  504.  
  505.  
  506. def test_main():
  507.     test_support.run_unittest(PosixPathTest)
  508.  
  509. if __name__ == '__main__':
  510.     test_main()
  511.  
  512.